package com.itlong.cloud.controller.logger;

import com.itlong.cloud.POJO.DTO.operate.OperateLoggerOperationLogGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateLoggerReleaseLogGetPageDTO;
import com.itlong.cloud.POJO.PO.OperateReleaseLogPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateReleaseLogVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.noncore.operate.IOperateReleaseLogServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      运营-日志管理-发布日志管理控制器
 * </desc>
 *
 * @createDate 2018/04/10
 */
@RestController
@RequestMapping("/logger/releaseLog")
public class OperateLoggerReleaseLogController {
    @Autowired
    IOperateReleaseLogServiceClient iOperateReleaseLogServiceClient;


    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @param dto 查询参数DTO
     * @return
     * @author Qiang.S
     * @createDate 2018/04/10
     */
    @RequestMapping(path = "getPage",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object getReleaseLogPage(OperateLoggerReleaseLogGetPageDTO dto){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto= HttpProtocolUtil.parseRequestParamToDTO(OperateLoggerReleaseLogGetPageDTO.class,request);
            Page<OperateReleaseLogVO> page=iOperateReleaseLogServiceClient.getPage(dto);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);

        } catch (Exception e) {
            throw new DataAccessException("【运营_日志管理_发布日志getPage】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      添加发布日志
     * </desc>
     *
     * @param operateReleaseLogPO 功能实体
     * @return
     * @author Qiang.S
     * @createDate 2018/04/10
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.SAVE_RELEASELOG_INFO_LOG_DESC)
    @RequestMapping(path = "save",method = RequestMethod.POST)
    @ResponseBody
    public Object saveReleaseLog(OperateReleaseLogPO operateReleaseLogPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateReleaseLogPO= HttpProtocolUtil.parseRequestParamToDTO(OperateReleaseLogPO.class,request);
            iOperateReleaseLogServiceClient.save(operateReleaseLogPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营_日志管理_发布日志save】添加功能失败",e);
        }
        return messageVO;

    }

    /**
     * <desc>
     *      修改发布日志
     * </desc>
     *
     * @param operateReleaseLogPO 发布日志实体
     * @return
     * @author Qiang.S
     * @createDate 2018/04/10
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_RELEASELOG_INFO_LOG_DESC)
    @RequestMapping(path = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object updateReleaseLog(OperateReleaseLogPO operateReleaseLogPO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateReleaseLogPO= HttpProtocolUtil.parseRequestParamToDTO(OperateReleaseLogPO.class,request);
            iOperateReleaseLogServiceClient.update(operateReleaseLogPO);
            messageVO=new MessageVO(operateReleaseLogPO.getId().toString(), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-_日志管理_发布日志update】更新发布日志失败", e);
        }
        return messageVO;
    }

}
